package com.ydkj.a37e_mall.Classes.Fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.baidu.navisdk.adapter.BNOuterLogUtil;
import com.baidu.navisdk.adapter.BNOuterTTSPlayerCallback;
import com.baidu.navisdk.adapter.BNRoutePlanNode;
import com.baidu.navisdk.adapter.BaiduNaviManager;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.ydkj.a37e_mall.Classes.Acitivity.Adapter.PopCityAdapter;
import com.ydkj.a37e_mall.Classes.Acitivity.Adapter.PopProvinceAdapter;
import com.ydkj.a37e_mall.Classes.Acitivity.Adapter.PopTypeAdapter;
import com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity.BNDemoGuideActivity;
import com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity.StoreDetail;
import com.ydkj.a37e_mall.Classes.Applicatiom.MyApplication;
import com.ydkj.a37e_mall.Classes.Model.Request.FindShopReq;
import com.ydkj.a37e_mall.Classes.Model.Request.FindShopTypeReq;
import com.ydkj.a37e_mall.Classes.Model.Request.GetProvinceReq;
import com.ydkj.a37e_mall.Classes.Model.Response.FindShopTypeResp;
import com.ydkj.a37e_mall.Classes.Model.Response.GetAddressCityResp;
import com.ydkj.a37e_mall.Classes.Model.Response.GetProvinceResp;
import com.ydkj.a37e_mall.Classes.Model.Response.GetShopListResp;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.Async_http_get;
import com.ydkj.a37e_mall.Resource.Utils.CommonUtil;
import com.ydkj.a37e_mall.Resource.Utils.Const;
import com.ydkj.a37e_mall.Resource.Utils.GsonImpl;
import com.ydkj.a37e_mall.Resource.Utils.Rsa;
import com.ydkj.a37e_mall.Resource.Utils.SharedPreferencesUtil;
import com.ydkj.a37e_mall.Resource.View.ClearEditText;
import com.ydkj.a37e_mall.Resource.View.CustomProgressDialog;

import org.json.JSONObject;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cz.msebera.android.httpclient.Header;

import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/1/4.
 * 发现
 */

public class FindShopFg extends BaseFg implements View.OnClickListener {
    private View rootView;
    private Activity mActivity;
    private SharedPreferencesUtil sharedPreferencesUtil;
    private MyApplication myApplication;
    private LinearLayout lly_type, lly_province, lly_city, lly_area;
    private TextView tv_type, tv_province, tv_city, tv_area, tv_search;
    private ImageView img_type, img_province, img_city, img_area;
    private ClearEditText clearEditText;
    private PullToRefreshListView lv_shop;
    private ShopAdapter shopAdapter;
    private PopTypeAdapter popTypeAdapter;
    private PopProvinceAdapter popProvinceAdapter;
    private PopCityAdapter popCityAdapter;

    private PopupWindow pop_type;
    private ListView lv_type;

    private PopupWindow pop_phone;
    private LinearLayout lly_pop_phone;
    private TextView tv_pop_phone, tv_pop_cancel;

    private String mSDCardPath = null;
    private static final String APP_FOLDER_NAME = "BNSDKDemo";
    public static final String ROUTE_PLAN_NODE = "routePlanNode";
    private String authinfo = null;
    private List<GetProvinceResp.ResultEntity.DataEntity> provinceList;
    private List<GetAddressCityResp.ResultEntity.DataEntity> cityList;
    private List<GetAddressCityResp.ResultEntity.DataEntity> areaList;
    private List<FindShopTypeResp.ResultEntity.DataEntity> typeList;
    private String province_code;
    private int city_code;
    private String type_id = "", province_id = "", city_id = "", area_id = "";
    private boolean is_show_type = false;
    private boolean is_show_province = false;
    private boolean is_show_city = false;
    private boolean is_show_area = false;
    private String page;
    private List<GetShopListResp.ResultEntity.DataEntity> shopDataEntityList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.findshop_fragment, container, false);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }

        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = getActivity();

        init();
        ShareSDK.initSDK(mActivity);
        BNOuterLogUtil.setLogSwitcher(true);//
        if (initDirs()) {
            Log.e("ssss", "initNavi");
            initNavi();
        }
    }

    private void init() {
        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(mActivity);
        myApplication = MyApplication.getInstance();
        lly_type = (LinearLayout) rootView.findViewById(R.id.lly_type);
        lly_province = (LinearLayout) rootView.findViewById(R.id.lly_province);
        lly_city = (LinearLayout) rootView.findViewById(R.id.lly_city);
        lly_area = (LinearLayout) rootView.findViewById(R.id.lly_area);

        tv_type = (TextView) rootView.findViewById(R.id.tv_type);
        tv_province = (TextView) rootView.findViewById(R.id.tv_province);
        tv_city = (TextView) rootView.findViewById(R.id.tv_city);
        tv_area = (TextView) rootView.findViewById(R.id.tv_area);
        tv_search = (TextView) rootView.findViewById(R.id.tv_search);

        img_type = (ImageView) rootView.findViewById(R.id.img_type);
        img_province = (ImageView) rootView.findViewById(R.id.img_province);
        img_city = (ImageView) rootView.findViewById(R.id.img_city);
        img_area = (ImageView) rootView.findViewById(R.id.img_area);

        typeList = new ArrayList<FindShopTypeResp.ResultEntity.DataEntity>();
        provinceList = new ArrayList<GetProvinceResp.ResultEntity.DataEntity>();
        cityList = new ArrayList<GetAddressCityResp.ResultEntity.DataEntity>();
        areaList = new ArrayList<GetAddressCityResp.ResultEntity.DataEntity>();
        clearEditText = (ClearEditText) mActivity.findViewById(R.id.et_search);

        GetShopList(REFRESH);


        shopDataEntityList = new ArrayList<>();
        shopAdapter = new ShopAdapter(shopDataEntityList);
        lv_shop = (PullToRefreshListView) rootView.findViewById(R.id.lv_shop);
        lv_shop.setAdapter(shopAdapter);
        lv_shop.setOnRefreshListener2(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                Log.e("ssss", "发现上拉");
                Log.e("ssss", "发现上拉==>" + sharedPreferencesUtil.getJingdu());
                Log.e("ssss", "发现上拉==>" + sharedPreferencesUtil.getWeidu());
                GetShopList(REFRESH);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                GetShopList(MORE);
            }
        });

        initPhonePopuWindow();
        initTypePopupWindow();
        tv_search.setOnClickListener(this);
        lly_type.setOnClickListener(this);
        lly_province.setOnClickListener(this);
        lly_city.setOnClickListener(this);
        lly_area.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.lly_type://类型
                if (is_show_type == false) {
                    tv_type.setTextColor(getResources().getColor(R.color.e50011));
                    img_type.setImageDrawable(getResources().getDrawable(R.mipmap.angle_red));
                    is_show_type = true;

                    popTypeAdapter = new PopTypeAdapter(mActivity, typeList);
                    lv_type.setAdapter(popTypeAdapter);
                    lv_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                            tv_type.setText(typeList.get(arg2).getName());
                            type_id = typeList.get(arg2).getId() + "";
                            if (type_id.equals("0")) {
                                province_id = "";
                                city_id = "";
                                area_id = "";

                                tv_province.setText("省份");

                                tv_city.setText("城市");
                                tv_area.setText("地区");
                            }
                            GetShopList(REFRESH);
                            pop_type.dismiss();
                            pop_type.setFocusable(false);
                        }
                    });
                    pop_type.setAnimationStyle(R.style.PopupAnimation);
                    pop_type.showAsDropDown(lly_area, 0, 5);
                    pop_type.update();
                } else if (is_show_type == true) {
                    tv_type.setTextColor(getResources().getColor(R.color.A333333));
                    img_type.setImageDrawable(getResources().getDrawable(R.mipmap.angle_default));
                    is_show_type = false;
                    pop_type.dismiss();
                    pop_type.setFocusable(false);
                }
                break;
            case R.id.lly_province://省份
                if (is_show_province == false) {
                    tv_province.setTextColor(getResources().getColor(R.color.e50011));
                    img_province.setImageDrawable(getResources().getDrawable(R.mipmap.angle_red));
                    is_show_province = true;

                    popProvinceAdapter = new PopProvinceAdapter(mActivity, provinceList);
                    lv_type.setAdapter(popProvinceAdapter);
                    lv_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                            tv_province.setText(provinceList.get(arg2).getName());
                            province_code = provinceList.get(arg2).getCode();
                            if (type_id.equals("9999")) {
                                type_id = "";
                            }
                            province_id = provinceList.get(arg2).getId() + "";
                            city_id = "";
                            area_id = "";
                            tv_city.setText("城市");
                            tv_area.setText("地区");
                            GetCity(province_code);
                            GetShopList(REFRESH);
                            Log.e("url", "province_id==" + province_id);
                            Log.e("url", "province_id==" + provinceList.get(arg2).getName());

                            pop_type.dismiss();
                            pop_type.setFocusable(false);

                        }
                    });

                    pop_type.setAnimationStyle(R.style.PopupAnimation);
                    pop_type.showAsDropDown(lly_area, 0, 5);
                    pop_type.update();
                } else if (is_show_province == true) {
                    tv_province.setTextColor(getResources().getColor(R.color.A333333));
                    img_province.setImageDrawable(getResources().getDrawable(R.mipmap.angle_default));
                    is_show_province = false;
                    pop_type.dismiss();
                    pop_type.setFocusable(false);
                }
                break;
            case R.id.lly_city://城市
                if (is_show_city == false) {

                    if (province_id.equals("")) {
                        CommonUtil.showToast(mActivity, "请先选择省份");
                        return;
                    }
                    tv_city.setTextColor(getResources().getColor(R.color.e50011));
                    img_city.setImageDrawable(getResources().getDrawable(R.mipmap.angle_red));
                    is_show_city = true;

                    popCityAdapter = new PopCityAdapter(mActivity, cityList);
                    lv_type.setAdapter(popCityAdapter);
                    lv_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                            tv_city.setText(cityList.get(arg2).getName());
                            city_code = cityList.get(arg2).getCode();

                            if (type_id.equals("9999")) {
                                type_id = "";
                            }
                            city_id = cityList.get(arg2).getId() + "";
                            Log.e("url", "city_id==" + city_id);
                            Log.e("url", "city_id==" + provinceList.get(arg2).getName());
                            area_id = "";
                            tv_area.setText("地区");
                            GetArea(city_code);
                            GetShopList(REFRESH);

                            pop_type.dismiss();
                            pop_type.setFocusable(false);
                        }
                    });

                    pop_type.setAnimationStyle(R.style.PopupAnimation);
                    pop_type.showAsDropDown(lly_area, 0, 5);
                    pop_type.update();
                } else {
                    tv_city.setTextColor(getResources().getColor(R.color.A333333));
                    img_city.setImageDrawable(getResources().getDrawable(R.mipmap.angle_default));
                    is_show_city = false;
                    pop_type.dismiss();
                    pop_type.setFocusable(false);
                }

                break;
            case R.id.lly_area://地区
                if (is_show_area == false) {

                    if (province_id.equals("")) {
                        CommonUtil.showToast(mActivity, "请先选择省份");
                        return;
                    } else if (city_id.equals("")) {
                        CommonUtil.showToast(mActivity, "请先选择城市");
                        return;
                    }
                    tv_area.setTextColor(getResources().getColor(R.color.e50011));
                    img_area.setImageDrawable(getResources().getDrawable(R.mipmap.angle_red));
                    is_show_area = true;

                    popCityAdapter = new PopCityAdapter(mActivity, areaList);
                    lv_type.setAdapter(popCityAdapter);
                    lv_type.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                            tv_area.setText(areaList.get(arg2).getName());
                            if (type_id.equals("9999")) {
                                type_id = "";
                            }
                            area_id = areaList.get(arg2).getId() + "";
                            Log.e("url", "area_id==" + area_id);
                            Log.e("url", "area_id==" + provinceList.get(arg2).getName());
                            GetShopList(REFRESH);
                            pop_type.dismiss();
                            pop_type.setFocusable(false);
                        }
                    });

                    pop_type.setAnimationStyle(R.style.PopupAnimation);
                    pop_type.showAsDropDown(lly_area, 0, 5);
                    pop_type.update();
                } else {
                    tv_area.setTextColor(getResources().getColor(R.color.A333333));
                    img_area.setImageDrawable(getResources().getDrawable(R.mipmap.angle_default));
                    is_show_area = false;
                    pop_type.dismiss();
                    pop_type.setFocusable(false);
                }
                break;
            case R.id.tv_search:
                GetShopList(REFRESH);
                break;
        }

    }

    private class ShopAdapter extends BaseAdapter {
        private List<GetShopListResp.ResultEntity.DataEntity> findDataEntityList;

        public ShopAdapter(List<GetShopListResp.ResultEntity.DataEntity> infos) {
            if (infos == null) {
                infos = new ArrayList<GetShopListResp.ResultEntity.DataEntity>();
            }
            findDataEntityList = infos;
        }

        @Override
        public int getCount() {
            return findDataEntityList.size();
        }

        @Override
        public GetShopListResp.ResultEntity.DataEntity getItem(int position) {
            return findDataEntityList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup viewGroup) {
            ViewHolder vh = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.findshop_fragment_listitem, null);
                vh = new ViewHolder();
                vh.lly_shop = (LinearLayout) convertView.findViewById(R.id.lly_shop);
                vh.img_shop = (ImageView) convertView.findViewById(R.id.img_shop);
                vh.tv_shop_name = (TextView) convertView.findViewById(R.id.tv_shop_name);
                vh.tv_shop_address = (TextView) convertView.findViewById(R.id.tv_shop_address);
                vh.tv_distance = (TextView) convertView.findViewById(R.id.tv_distance);
                vh.divide = (TextView) convertView.findViewById(R.id.divide);
                vh.lly_phone = (LinearLayout) convertView.findViewById(R.id.lly_phone);
                vh.lly_gps = (LinearLayout) convertView.findViewById(R.id.lly_gps);
                vh.lly_share = (LinearLayout) convertView.findViewById(R.id.lly_share);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            convertView.setTag(vh);
            convertView.setClickable(true);
            if (position > 0) {
                vh.divide.setVisibility(View.VISIBLE);
            }

            ImageLoader.getInstance().displayImage(getItem(position).getThumb(), vh.img_shop);
            vh.tv_shop_name.setText(getItem(position).getName());
            vh.tv_shop_address.setText(getItem(position).getDetail());


//            if (getItem(position).getDistance() < 1000) {
//                vh.tv_distance.setText(getItem(position).getDistance() + "米");
//            } else {
//                DecimalFormat df = new DecimalFormat("#.00");
//                final String distance = df.format((getItem(position).getDistance() / 1000.0));
//                vh.tv_distance.setText(distance + "km");
//            }


            //导航
            vh.lly_gps.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("url", "111");
                    if (BaiduNaviManager.isNaviInited()) {
//                        Toast.makeText(mActivity, "百度导航即将开始", Toast.LENGTH_SHORT).show();

                        Log.e("ssss", "lat==>" + getItem(position).getLat());
                        Log.e("ssss", "lng==>" + getItem(position).getLng());
                        routeplanToNavi(Double.parseDouble(getItem(position).getLng()), Double.parseDouble(getItem(position).getLat()), BNRoutePlanNode.CoordinateType.GCJ02);
//
//                        routeplanToNavi(116.377248, 40.01337, BNRoutePlanNode.CoordinateType.GCJ02);

                    }
                }
            });


            //电话
            vh.lly_phone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    tv_pop_phone.setText(getItem(position).getTel());
                    pop_phone.setAnimationStyle(R.style.popupWindowAnimation);
                    pop_phone.showAtLocation(mActivity.findViewById(R.id.lly_main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                    pop_phone.update();
                }
            });

            //分享
            vh.lly_share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

//                    showShare("http://sharesdk.cn", getItem(position).getName());
                    showShare(getItem(position).getUrl(), getItem(position).getName());
                }
            });


            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mActivity, StoreDetail.class);
//                    Intent intent = new Intent(mActivity, MainActivity.class);
//                    intent.putExtra("tel",getItem(position).getTel());
//                    intent.putExtra("jingdu", getItem(position).getLng());
//                    intent.putExtra("weidu", getItem(position).getLat());
//                    intent.putExtra("detail_url", getItem(position).getDetail_url());
//                    intent.putExtra("share_url", getItem(position).getShare_url());
                    intent.putExtra("shopDataEntityList", (Serializable) shopDataEntityList);
                    Log.e("url", "findshop_tel" + getItem(position).getTel());
                    Log.e("url", "findshop_jingdu" + getItem(position).getLng());
                    Log.e("url", "findshop_weidu" + getItem(position).getLat());
//                    Log.e("url", "findshop_detail_url" + getItem(position).getDetail_url());
//                    Log.e("url", "findshop_share_url" + getItem(position).getShare_url());
                    startActivity(intent);
                }
            });
            return convertView;
        }

        public class ViewHolder {
            private LinearLayout lly_shop, lly_phone, lly_gps, lly_share;
            private ImageView img_shop;
            private TextView tv_shop_name, tv_shop_address, tv_distance, divide;
        }
    }

    //电话的popupwindow
    private void initPhonePopuWindow() {

        LayoutInflater inflater = LayoutInflater.from(mActivity);
        View layout = inflater.inflate(R.layout.pop_phone, null);
        lly_pop_phone = (LinearLayout) layout.findViewById(R.id.lly_pop_phone);
        tv_pop_phone = (TextView) layout.findViewById(R.id.tv_pop_phone);
        tv_pop_cancel = (TextView) layout.findViewById(R.id.tv_pop_cancel);
        layout.invalidate();
        pop_phone = new PopupWindow(layout, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        pop_phone.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
        pop_phone.setBackgroundDrawable(new BitmapDrawable());
        pop_phone.setOutsideTouchable(true);
        pop_phone.setFocusable(true);

        lly_pop_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + tv_pop_phone.getText().toString());
                intent.setData(data);
                startActivity(intent);
                pop_phone.dismiss();
                pop_phone.setFocusable(false);
            }
        });

        tv_pop_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop_phone.dismiss();
                pop_phone.setFocusable(false);
            }
        });

    }

    //类型的popupwindow
    private void initTypePopupWindow() {
        LayoutInflater inflater = LayoutInflater.from(mActivity);
        View layout = inflater.inflate(R.layout.pop_type, null);
        lv_type = (ListView) layout.findViewById(R.id.lv_type);
        layout.invalidate();
        pop_type = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, 500);
        pop_type.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                tv_type.setTextColor(getResources().getColor(R.color.A333333));
                img_type.setImageDrawable(getResources().getDrawable(R.mipmap.angle_default));
                tv_province.setTextColor(getResources().getColor(R.color.A333333));
                img_province.setImageDrawable(getResources().getDrawable(R.mipmap.angle_default));
                tv_city.setTextColor(getResources().getColor(R.color.A333333));
                img_city.setImageDrawable(getResources().getDrawable(R.mipmap.angle_default));
                tv_area.setTextColor(getResources().getColor(R.color.A333333));
                img_area.setImageDrawable(getResources().getDrawable(R.mipmap.angle_default));
            }
        });
        pop_type.setBackgroundDrawable(new BitmapDrawable());
        pop_type.setOutsideTouchable(true);
        pop_type.setFocusable(true);

    }

    //获取省份列表
    private void GetProvince() {
//        CustomProgressDialog.createDialog(mActivity).Show();
        GetProvinceReq req = new GetProvinceReq();
        req.setType("province");
        req.setParent("0");
        String url = Const.COMMON_ADDRESS + req.urlString();
        try {
            String source = req.getString();
            Rsa rsa = new Rsa();
            secret = rsa.encryptByPublic(source);
        } catch (Exception e) {
            e.printStackTrace();
        }
        RequestParams params = new RequestParams();
        params.put("secret", secret);
//        Log.e("url", "choose_province_params==" + params);

        Async_http_get.get(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
//                CustomProgressDialog.Dismiss();
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                Log.e("url", "choose_province_json==" + response);
                if (errno.equals("000")) {
                    GetProvinceResp resp = GsonImpl.get().toObject(response.toString(), GetProvinceResp.class);
                    //把数据填入Resp后，再把数据填入list里
                    provinceList.clear();
                    provinceList.addAll(resp.getResult().getData());
                }
                if (!errno.equals("000")) {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Log.e("url", "province_statusCode==" + statusCode);
                CustomProgressDialog.Dismiss();

                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }

    //获取城市列表
    private void GetCity(String code) {
//        CustomProgressDialog.createDialog(mActivity).Show();
        GetProvinceReq req = new GetProvinceReq();
        req.setType("city");
        req.setParent(code + "");
        String url = Const.COMMON_ADDRESS + req.urlString();
        try {
            String source = req.getString();
            Rsa rsa = new Rsa();
            secret = rsa.encryptByPublic(source);
        } catch (Exception e) {
            e.printStackTrace();
        }

        params.put("secret", secret);
        Log.e("url", "choose_city_params==" + params);

        Async_http_get.get(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Log.e("url", "city_statusCode__onSuccess==" + statusCode);
//                CustomProgressDialog.Dismiss();
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                Log.e("url", "response==" + response);
                if (errno.equals("000")) {
                    GetAddressCityResp resp = GsonImpl.get().toObject(response.toString(), GetAddressCityResp.class);
                    //把数据填入Resp后，再把数据填入list里
                    cityList.clear();
                    cityList.addAll(resp.getResult().getData());
                }
                if (!errno.equals("000")) {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Log.e("url", "city_statusCode==" + statusCode);
                Log.e("url", "responseString==" + responseString);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }

    //获取地区列表
    private void GetArea(int code) {
//        CustomProgressDialog.createDialog(mActivity).Show();
        GetProvinceReq req = new GetProvinceReq();
        req.setType("area");
        req.setParent(code + "");
        String url = Const.COMMON_ADDRESS + req.urlString();
        try {
            String source = req.getString();
            Rsa rsa = new Rsa();
            secret = rsa.encryptByPublic(source);
        } catch (Exception e) {
            e.printStackTrace();
        }

        params.put("secret", secret);

        Log.e("url", "choose_area_params==" + params);
        Async_http_get.get(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
//                CustomProgressDialog.Dismiss();
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                Log.e("url", "area_json==" + response);
                if (errno.equals("000")) {
                    GetAddressCityResp resp = GsonImpl.get().toObject(response.toString(), GetAddressCityResp.class);
                    //把数据填入Resp后，再把数据填入list里
                    areaList.clear();
                    areaList.addAll(resp.getResult().getData());
                }
                if (!errno.equals("000")) {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }


    //获取门店
    private void GetShopList(final int type) {

        FindShopReq req = new FindShopReq();
        if (myApplication.getDataEntity() != null) {
            Log.e("ssss", "111111==>");

            CustomProgressDialog.createDialog(mActivity).show();
            req.setUuid(myApplication.getDataEntity().getUuid());
        } else {
            req.setUuid("");
//            if (shopDataEntityList != null) {
//                shopDataEntityList.clear();
//            }
//            if (shopAdapter != null) {
//                shopAdapter.notifyDataSetChanged();
//
//            }
//            if (lv_shop != null) {
//                lv_shop.onRefreshComplete();
//            }
//            Log.e("ssss", "22222==>");
//            return;
        }

        if (type == LOAD || type == REFRESH) {
            page = "1";
        } else {
            page = (shopDataEntityList.size() / 10 + 1) + "";
        }
        req.setPage(page);
        if (sharedPreferencesUtil.getJingdu().equals("0") || sharedPreferencesUtil.getWeidu().equals("0")) {
            req.setLat("0");
            req.setLng("0");
        } else {
            req.setLat(sharedPreferencesUtil.getWeidu());
            req.setLng(sharedPreferencesUtil.getJingdu());
//            req.setLat("24.49358");
//            req.setLng("118.191569");
        }
        req.setPage_size("10");


        req.setType("1");
        req.setProduct_id("0");
        req.setIndustry(type_id);
        Log.e("ssss", "province_id==>" + province_id);
        Log.e("ssss", "city_id==>" + city_id);
        Log.e("ssss", "area_id==>" + area_id);

        req.setProvince_id(province_id);
        req.setCity_id(city_id + "");
        req.setArea_id(area_id);
        log.e("ssss", "找店搜索==》" + clearEditText.getText().toString());

        if (clearEditText.getText().toString().equals("")) {
            req.setKeyword("");
        } else {
            req.setKeyword(clearEditText.getText().toString());
        }
        RequestParams params = new RequestParams();

        params.put("secret", req.getSecret());

        Async_http_get.get(Const.COMMON_ADDRESS + req.urlString(), params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                lv_shop.onRefreshComplete();
                Log.e("url", "json==" + response);
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                if (errno.equals("000")) {
                    GetShopListResp resp = GsonImpl.get().toObject(response.toString(), GetShopListResp.class);
                    if (type == LOAD || type == REFRESH) {
                        shopDataEntityList.clear();
                        shopDataEntityList.addAll(resp.getResult().getData());
                    } else {
                        shopDataEntityList.addAll(resp.getResult().getData());
                    }
                    if (shopDataEntityList.size() % 10 > 0) {
                        lv_shop.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                    } else {
                        lv_shop.setMode(PullToRefreshBase.Mode.BOTH);
                    }
                    shopAdapter.notifyDataSetChanged();
                    GetProvince();
                    typeList.clear();
                    GetFindShopType();
                } else if (errno.equals("003")) {
                    CommonUtil.showToast(mActivity, errmsg);
                    shopDataEntityList.clear();
                    shopAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
                lv_shop.onRefreshComplete();
            }
        });
    }

    //获取类型
    private void GetFindShopType() {
        CustomProgressDialog.createDialog(mActivity).Show();
        FindShopTypeReq req = new FindShopTypeReq();

        if (myApplication.getDataEntity() == null) {
            req.setUuid("");

        } else {
            req.setUuid(myApplication.getDataEntity().getUuid());

        }
        req.setType("0");
        req.setPage("1");
        req.setPage_size("10");

        RequestParams params = new RequestParams();
        params.put("secret", req.getSecret());
        Log.e("url", "params11111==" + params);
        Async_http_get.get(Const.COMMON_ADDRESS + req.urlString(), params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                Log.e("url", "type_json==" + response.toString());
                if (errno.equals("000")) {
                    FindShopTypeResp resp = GsonImpl.get().toObject(response.toString(), FindShopTypeResp.class);
                    FindShopTypeResp.ResultEntity.DataEntity q = new FindShopTypeResp.ResultEntity.DataEntity();
                    q.setId("0");
                    q.setName("全部");
                    typeList.add(q);
                    typeList.addAll(resp.getResult().getData());
                } else {
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
//                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }

    //分享
    private void showShare(String url, String text) {
        ShareSDK.initSDK(getActivity());

        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle(text);
        // titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl(url);
        // text是分享文本，所有平台都需要这个字段
        oks.setText(text);
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(url);
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(url);

        // 启动分享GUI
        oks.show(mActivity);
    }

    private void initNavi() {

        BNOuterTTSPlayerCallback ttsCallback = null;

        BaiduNaviManager.getInstance().init(mActivity, mSDCardPath, APP_FOLDER_NAME,
                new BaiduNaviManager.NaviInitListener() {
                    @Override
                    public void onAuthResult(int status, final String msg) {
                        if (0 == status) {
                            authinfo = "key校验成功!";
                            Log.e("ssss", "key校验成功");
                        } else {
                            authinfo = "key校验失败, " + msg;
                            Log.e("ssss", "key校验失败");
                        }
                        mActivity.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
//                                Toast.makeText(mActivity, authinfo, Toast.LENGTH_LONG).show();
                                Log.e("ssss", "Map_authinfo==" + authinfo);
                            }
                        });

                    }

                    public void initSuccess() {
//                        Toast.makeText(mActivity, "百度导航引擎初始化成功", Toast.LENGTH_SHORT).show();
                        Log.e("ssss", "Map_百度导航引擎初始化成功");
                    }

                    public void initStart() {
//                        Toast.makeText(mActivity, "百度导航引擎初始化开始", Toast.LENGTH_SHORT).show();
                        Log.e("ssss", "Map_百度导航引擎初始化开始");
                    }

                    public void initFailed() {
//                        Toast.makeText(mActivity, "百度导航引擎初始化失败", Toast.LENGTH_SHORT).show();
                        Log.e("ssss", "Map_百度导航引擎初始化失败");
                    }
                }, null /*mTTSCallback*/);
    }

    private boolean initDirs() {
        mSDCardPath = getSdcardDir();
        if (mSDCardPath == null) {
            return false;
        }
        File f = new File(mSDCardPath, APP_FOLDER_NAME);
        if (!f.exists()) {
            try {
                f.mkdir();
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    private String getSdcardDir() {
        if (Environment.getExternalStorageState().equalsIgnoreCase(
                Environment.MEDIA_MOUNTED)) {
            return Environment.getExternalStorageDirectory().toString();
        }
        return null;
    }

    public void routeplanToNavi(double jingdu, double weidu, BNRoutePlanNode.CoordinateType coType) {
        BNRoutePlanNode sNode = null;
        BNRoutePlanNode eNode = null;
        Log.e("url", "目的地址:经度==" + jingdu + " 纬度==" + weidu);

        switch (coType) {
            case GCJ02: {
                Log.e("ssss", "导航");
                sNode = new BNRoutePlanNode(Double.parseDouble(sharedPreferencesUtil.getJingdu()), Double.parseDouble(sharedPreferencesUtil.getWeidu()), "目前地址", null, coType);

                eNode = new BNRoutePlanNode(jingdu, weidu, "目的地", null, coType);
                Log.e("url", "sNode==" + sNode + "   eNode==" + eNode);
                Log.e("url", "当前地址:经度==" + sharedPreferencesUtil.getWeidu() + " 纬度==" + sharedPreferencesUtil.getJingdu());
                Log.e("url", "目的地址:经度==" + jingdu + " 纬度==" + weidu);
                break;
            }
//            case WGS84: {
//                sNode = new BNRoutePlanNode(116.300821, 40.050969,
//                        "百度大厦", null, coType);
//                eNode = new BNRoutePlanNode(116.397491, 39.908749,
//                        "北京天安门", null, coType);
//                break;
//            }
//            case BD09_MC: {
//                sNode = new BNRoutePlanNode(12947471, 4846474,
//                        "百度大厦", null, coType);
//                eNode = new BNRoutePlanNode(12958160, 4825947,
//                        "北京天安门", null, coType);
//                break;
//            }
            default:

        }
        if (sNode != null && eNode != null) {
            List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
            list.add(sNode);
            list.add(eNode);
            BaiduNaviManager.getInstance().launchNavigator(mActivity, list, 1, true, new DemoRoutePlanListener(sNode));
        }
    }

    public class DemoRoutePlanListener implements BaiduNaviManager.RoutePlanListener {

        private BNRoutePlanNode mBNRoutePlanNode = null;

        public DemoRoutePlanListener(BNRoutePlanNode node) {
            Log.e("url", "DemoRoutePlanListener");
            mBNRoutePlanNode = node;
            Log.e("url", "mBNRoutePlanNode==" + mBNRoutePlanNode);
        }

        @Override
        public void onJumpToNavigator() {
            Log.e("url", "onJumpToNavigator_BNDemoGuideActivity");
            Intent intent = new Intent(mActivity, BNDemoGuideActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable(ROUTE_PLAN_NODE, (BNRoutePlanNode) mBNRoutePlanNode);
            intent.putExtras(bundle);
            startActivity(intent);
        }

        @Override
        public void onRoutePlanFailed() {
            // TODO Auto-generated method stub
            Log.e("url", "onRoutePlanFailed");
        }

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        ShareSDK.stopSDK(getActivity());
    }
}

